﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using SearchEngine;

namespace SilverlightApplication1
{
    public partial class Page : UserControl
    {
        // Atributo sobre el que ejecutaremos las busquedas
        private Searcher searcher;

        private List<Common.DataTypes.Item> lstQueryResult;

        public Page()
        {
            InitializeComponent();

            // Se crea el Searcher y se crea el evento que maneja la finalizacion de la busqueda
            // Create searcher and event to handle search finalization event
            this.searcher = new Searcher(MainDispatcher.Current);
            searcher.SearchCompleted += new Searcher.SearchCompletedHandler(searcher_SearchCompleted);

            lstQueryResult = new List<Common.DataTypes.Item>();

            // This Boolean allows to manage the Advanced Search section
            blnAvanced = true;
        }

        private Boolean blnAvanced;

        /// <summary>
        /// Search information about 'txtQuery' and load it in 'lstResult'
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="args"></param>
        /// 
        void SearcherFnc()
        {
            this.lstResult.Items.Clear();
            // Amazon Search parameter
            string keyword = this.txtQuery.Text;

            try
            {
                // Perform Amazon search
                searcher.PerformSearch(keyword);
            }

            catch (Exception e1)
            {
                // TODO manejar excepcion
            }        
        }

        void Search_Click(object sender, EventArgs args)
        {            
            SearcherFnc();
        }

        // Function to handle search complete event
        void searcher_SearchCompleted(object sender, SearchCompletedEventArgs e)
        {
            /*this.textprueba.Text = "DESPUES DE TERMINAR: ";

            Common.DataTypes.Book book;
            foreach (Common.DataTypes.Item i in e.Result)
            {
                book = (Common.DataTypes.Book)i;
                textprueba.Text += book.Author + " ";
            }*/
            
            Common.DataTypes.Book b;

            lstQueryResult.Clear();
            foreach (Common.DataTypes.Item i in e.Result)
            {
                //Add the Item to our local variable
                lstQueryResult.Add(i);
            }

            //In future this will be relative to the page
            for (int Index = 0; Index<5; Index++)
            {
                b = (Common.DataTypes.Book)lstQueryResult[Index];

                ListBoxItem item = new ListBoxItem();
                ListItemControl lstItemCtrl = new ListItemControl();
                lstItemCtrl.Tag = Index;
                lstItemCtrl.imgData.MouseLeftButtonDown += new MouseButtonEventHandler(ShowItemInformation);
                lstItemCtrl.imgData.Source = new BitmapImage(new Uri(b.MediumImage, UriKind.Absolute));
                lstItemCtrl.txtInfo1.Text = b.Author;
                lstItemCtrl.txtInfo2.Text = b.Title;
                lstItemCtrl.txtInfo3.Text = b.Publisher;
                lstItemCtrl.txtInfo4.Text = "Price: " + b.ListPrice.CurrencyCode + " " + b.ListPrice.FormattedPrice;
                item.Content = lstItemCtrl;
                lstResult.Items.Add(item);
            }

            lstResult.Visibility = Visibility.Visible;
        }

        void txtQueryKeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == System.Windows.Input.Key.Enter)
            {
                SearcherFnc();
            }
        }

        void ShowItemInformation(object sender, MouseButtonEventArgs e)
        {
            ListItemControl Item = ((ListItemControl)((Grid)((Image)sender).Parent).Parent);
            
            Common.DataTypes.Book b = ((Common.DataTypes.Book)lstQueryResult[((int)Item.Tag)]);

            imgItemInformation.Source = new BitmapImage(new Uri(b.LargeImage, UriKind.Absolute));
            txtItemInfo1.Text = b.Author;            
            txtItemInfo2.Text = b.Publisher;
            txtItemInfo3.Text = b.Title;
            
            infoItemVisible.Begin();
        }

        void CloseItemInformation(object sender, EventArgs args)
        {
            infoItemNotVisible.Begin();
        }
        
        void btnBooksToBigger(object sender, EventArgs args)
        {
            // Case the storyboard is still executing on other button, it's stoped
            renderBigger.Stop();

            Storyboard.SetTargetName(renderBigger, "rdrBooks");
            rdrBooks.CenterX = 30;
            rdrBooks.CenterY = 30;
            renderBigger.Begin();
        }

        void btnBooksToLower(object sender, EventArgs args)
        {
            // Case the storyboard is still executing on other button, it's stoped
            renderLower.Stop();

            Storyboard.SetTargetName(renderLower, "rdrBooks");
            renderLower.Begin();
        }

        void btnAbout1ToBigger(object sender, EventArgs args)
        {
            // Case the storyboard is still executing on other button, it's stoped
            renderBigger.Stop();

            Storyboard.SetTargetName(renderBigger, "rdrAbout1");
            rdrAbout1.CenterX = 30;
            rdrAbout1.CenterY = 30;
            renderBigger.Begin();
        }

        void btnAbout1ToLower(object sender, EventArgs args)
        {
            // Case the storyboard is still executing on other button, it's stoped
            renderLower.Stop();

            Storyboard.SetTargetName(renderLower, "rdrAbout1");
            renderLower.Begin();
        }

        void btnAbout2ToBigger(object sender, EventArgs args)
        {
            // Case the storyboard is still executing on other button, it's stoped
            renderBigger.Stop();

            Storyboard.SetTargetName(renderBigger, "rdrAbout2");
            rdrAbout2.CenterX = 30;
            rdrAbout2.CenterY = 30;
            renderBigger.Begin();
        }

        void btnAbout2ToLower(object sender, EventArgs args)
        {
            // Case the storyboard is still executing on other button, it's stoped
            renderLower.Stop();

            Storyboard.SetTargetName(renderLower, "rdrAbout2");
            renderLower.Begin();
        }
        void Advanced(object sender, EventArgs args)
        {
            if (blnAvanced)
            {
                toVisible.Begin();
            }
            else
            {
                toNotVisible.Begin();
            }
            blnAvanced = !blnAvanced;
            
        }
        
        void btnConfirmToLight(object sender, EventArgs args)
        {
            imgDarkGreen.Visibility = Visibility.Collapsed;
            imgLightGreen.Visibility = Visibility.Visible;
        }

        void btnConfirmToDark(object sender, EventArgs args)
        {
            imgDarkGreen.Visibility = Visibility.Visible;
            imgLightGreen.Visibility = Visibility.Collapsed;
        }

        void btnCancelToLight(object sender, EventArgs args)
        {
            imgDarkRed.Visibility = Visibility.Collapsed;
            imgLightRed.Visibility = Visibility.Visible;
        }

        void btnCancelToDark(object sender, EventArgs args)
        {
            imgDarkRed.Visibility = Visibility.Visible;
            imgLightRed.Visibility = Visibility.Collapsed;
        }
    }
}
